package com.baofu.paycore.biz.paycore.converter;

import com.baofu.paycore.biz.engine.command.impl.*;
import com.baofu.paycore.common.enums.CardTypeEnum;
import com.baofu.paycore.manager.model.PayBO;
import com.baofu.paycore.manager.model.RefundBO;
import com.baofu.paycore.manager.model.SubPayBO;
import com.baofu.paycore.manager.model.SubRefundBO;
import com.baofu.paycore.service.facade.enums.RequestSystemEnum;
import org.apache.commons.lang.StringUtils;
import org.slf4j.MDC;
import org.slf4j.helpers.SystemMarker;

/**
 * 支付指令转换器
 * <p>
 * </p>
 * User: kwl Date: 2016/6/12 ProjectName: paycore Version: 5.0.0
 */
public class CommandConverter {

    /**
     * 余额支付命令转换
     *
     * @param subPayBO    支付资金明细
     * @param payBO       支付主单信息
     */
    public static BalanceCommand balanceCommandConverter(SubPayBO subPayBO, PayBO payBO){

        BalanceCommand balanceCommand = new BalanceCommand();

        balanceCommand.setRelationNo(payBO.getRelationNo());
        balanceCommand.setRequestOrderNo(payBO.getPayNo());
        balanceCommand.setRequestBizNo(subPayBO.getFundDetailNo());
        balanceCommand.setRequestSystem(payBO.getRequestSystem());
        balanceCommand.setRequestDate(payBO.getRequestDate());
        balanceCommand.setServiceCode(payBO.getServiceCode());
        balanceCommand.setPayNo(payBO.getPayNo());

        balanceCommand.setBizChargeAmt(subPayBO.getFundAmt());
        balanceCommand.setBizType(payBO.getBizType());
        balanceCommand.setSubBizType(payBO.getSubBizType());
        balanceCommand.setFundingChannel(subPayBO.getFundingChannel());
        balanceCommand.setSubFundingChannel(subPayBO.getSubFundingChannel());

        balanceCommand.setPayeeAccNo(subPayBO.getPayeeAccNo());
        balanceCommand.setPayerAccNo(subPayBO.getPayerAccNo());
        balanceCommand.setCcy(subPayBO.getCcy());
        balanceCommand.setAmount(subPayBO.getPayToolAmt());

        balanceCommand.setAsyPayeeFlag(Boolean.TRUE);
        balanceCommand.setFundDetailNo(subPayBO.getFundDetailNo());

        balanceCommand.setEventNo(subPayBO.getEventNo());
        balanceCommand.setOperator(payBO.getOperator());
        balanceCommand.setRemark(subPayBO.getDescription());
        balanceCommand.setTraceLogId(payBO.getTradeLogId());

        return balanceCommand;
    }

    /**
     * 调用银行指令
     * <p>
     * 请求时间的设定：将前端请求的requestDate作为银行单调用充值接口请求时间，
     * 方便后面根据requestDate、requestSystem、requestBizNo查询唯一充值请求
     * </p>
     * @param subPayBO    付款资金单
     * @param payBO       付款主单
     */
    public static BankCommand bankCommandConverter(SubPayBO subPayBO, PayBO payBO){

        BankCommand bankCommand = new BankCommand();

        bankCommand.setRelationNo(payBO.getRelationNo());
        bankCommand.setRequestOrderNo(payBO.getPayNo());
        bankCommand.setRequestBizNo(subPayBO.getFundDetailNo());
        bankCommand.setRequestDate(payBO.getRequestDate());
        bankCommand.setRequestSystem(payBO.getRequestSystem());
        bankCommand.setRequestChannel(payBO.getRequestChannel());

        bankCommand.setBizType(payBO.getBizType());
        bankCommand.setSubBizType(payBO.getSubBizType());
        bankCommand.setPayTool(subPayBO.getPayToolType());
        bankCommand.setSubPayTool(subPayBO.getPayToolSubType());

        bankCommand.setContractNo(payBO.getPayerNo());
        bankCommand.setPayeeAccNo(payBO.getPayerAccNo());
        bankCommand.setCcy(payBO.getCcy());
        bankCommand.setAmount(subPayBO.getPayToolAmt());

        bankCommand.setAgreeRelId(subPayBO.getAgreeRelId());
        bankCommand.setAgreementNo(subPayBO.getAgreementNo());
        bankCommand.setBankCode(subPayBO.getBankCode());
        bankCommand.setOpFlag(subPayBO.getOpFlag());
        if(StringUtils.isNotEmpty(subPayBO.getCardType())){
            bankCommand.setCardType(CardTypeEnum.getByCode(subPayBO.getCardType()));

        }
        bankCommand.setMerchantNo(payBO.getMerchantNo());
        bankCommand.setOperator(payBO.getOperator());
        bankCommand.setTraceLogId(payBO.getTradeLogId());
        bankCommand.setDescription(payBO.getDescription());
        bankCommand.setReturnUrl(payBO.getReturnUrl());
        bankCommand.setPaymentExt(payBO.getPaymentExt());
        
        return bankCommand;
    }

    /**
     * 调用银行确认指令
     * <p>
     * 根据requestDate、requestSystem、requestBizNo查询唯一充值请求
     * </p>
     * @param subPayBO    付款资金单
     * @param payBO       付款主单
     */
    public static BankConfirmCommand bankConfirmCommandConverter(SubPayBO subPayBO, PayBO payBO){

        BankConfirmCommand bankCommand = new BankConfirmCommand();

        bankCommand.setRequestOrderNo(payBO.getPayNo());
        bankCommand.setRequestBizNo(subPayBO.getFundDetailNo());
        bankCommand.setRequestSystem(payBO.getRequestSystem());
        bankCommand.setTraceLogId(payBO.getTradeLogId());
        bankCommand.setSmsCode(payBO.getSmsCode());
        return bankCommand;
    }



    /**
     * 退款余额支付命令转换
     *
     * @param subRefundBO    支付资金明细
     * @param refundBO       支付主单信息
     */
    public static RefundBalCommand refundBalanceCommandConverter(SubRefundBO subRefundBO, RefundBO refundBO){

        RefundBalCommand refundBalCommand = new RefundBalCommand();

        refundBalCommand.setRelationNo(refundBO.getRelationNo());
        refundBalCommand.setRequestOrderNo(subRefundBO.getPayNo());
        refundBalCommand.setRequestBizNo(subRefundBO.getFundDetailNo());
        refundBalCommand.setRequestSystem(RequestSystemEnum.PAYCORE.getCode());
        refundBalCommand.setRequestDate(refundBO.getRequestDate());

        //设置原余额支付单的凭证号和会计日
        refundBalCommand.setOrigReceiptNo(subRefundBO.getOrigTransReceiptNo());
        refundBalCommand.setOrigAccDate(subRefundBO.getOrigTransDate());

        refundBalCommand.setBizType(subRefundBO.getBizType());
        refundBalCommand.setSubBizType(subRefundBO.getSubBizType());
        refundBalCommand.setFundingChannel(subRefundBO.getFundingChannel());
        refundBalCommand.setSubFundingChannel(subRefundBO.getSubFundingChannel());

        refundBalCommand.setPayeeAccNo(subRefundBO.getPayeeAccNo());
        refundBalCommand.setPayerAccNo(subRefundBO.getPayerAccNo());
        refundBalCommand.setCcy(subRefundBO.getCcy());
        refundBalCommand.setAmount(subRefundBO.getAmt());

        refundBalCommand.setAsyPayeeFlag(Boolean.FALSE);
        refundBalCommand.setFundDetailNo(subRefundBO.getFundDetailNo());

        refundBalCommand.setEventNo(subRefundBO.getEventNo());
        refundBalCommand.setOperator(subRefundBO.getOperator());
        refundBalCommand.setRemark(subRefundBO.getDescription());
        refundBalCommand.setTraceLogId(refundBO.getTradeLogId());
        refundBalCommand.setDescription(refundBO.getDescription());

        return refundBalCommand;
    }

    /**
     * 退款调用银行指令
     * <p>
     * 请求时间的设定：将前端请求的requestDate作为银行单调用充值接口请求时间，
     * 方便后面根据requestDate、requestSystem、requestBizNo查询唯一充值请求
     * </p>
     * @param subRefundBO    付款资金单
     * @param refundBO       付款主单
     */
    public static RefundBankCommand refundBankCommandConverter(SubRefundBO subRefundBO, RefundBO refundBO){

        RefundBankCommand refundBankCommand = new RefundBankCommand();

        refundBankCommand.setRelationNo(refundBO.getRelationNo());
        refundBankCommand.setRequestOrderNo(subRefundBO.getPayNo());
        refundBankCommand.setRequestBizNo(subRefundBO.getFundDetailNo());
        refundBankCommand.setRequestDate(refundBO.getRequestDate());
        refundBankCommand.setRequestSystem(RequestSystemEnum.PAYCORE.getCode());
        refundBankCommand.setRequestChannel(refundBO.getRequestChannel());

        //设置原银行卡支付单的请求系统、请求单号、请求日期
        refundBankCommand.setOrigRequestOrderNo(subRefundBO.getOrigRequestOrderNo());
        refundBankCommand.setOrigRequestBizNo(subRefundBO.getOrigRequestBizNo());
        refundBankCommand.setOrigRequestSystem(subRefundBO.getOrigRequestSystem());
        refundBankCommand.setOrigRequestDate(subRefundBO.getOrigRequestDate());

        refundBankCommand.setBizType(subRefundBO.getBizType());
        refundBankCommand.setSubBizType(subRefundBO.getSubBizType());
        refundBankCommand.setPayTool(subRefundBO.getPayToolType());
        refundBankCommand.setSubPayTool(subRefundBO.getPayToolSubType());

        //refundBankCommand.setContractNo(refundBO.getPayerNo());
        refundBankCommand.setPayeeAccNo(subRefundBO.getPayerAccNo());
        refundBankCommand.setCcy(subRefundBO.getCcy());
        refundBankCommand.setAmount(subRefundBO.getAmt());

        refundBankCommand.setAgreeRelId(subRefundBO.getAgreeRelId());
        refundBankCommand.setAgreementNo(subRefundBO.getAgreementNo());

        refundBankCommand.setBankCode(subRefundBO.getBankCode());
        if(StringUtils.isNotEmpty(subRefundBO.getCardType())){
            refundBankCommand.setCardType(CardTypeEnum.getByCode(subRefundBO.getCardType()));

        }
        refundBankCommand.setDescription(refundBO.getDescription());
        refundBankCommand.setTraceLogId(refundBO.getTradeLogId());
        refundBankCommand.setOperator(subRefundBO.getOperator());
        return refundBankCommand;
    }

    /**
     * 退款业务收费资金单构建
     *
     * @param subRefundBO    支付资金明细
     * @param refundBO       支付主单信息
     */
    public static BalanceCommand balanceCommandConverter(SubRefundBO subRefundBO, RefundBO refundBO){

        BalanceCommand balanceCommand = new BalanceCommand();

        balanceCommand.setRelationNo(refundBO.getRelationNo());
        balanceCommand.setRequestOrderNo(subRefundBO.getPayNo());
        balanceCommand.setRequestBizNo(subRefundBO.getFundDetailNo());
        balanceCommand.setRequestSystem(RequestSystemEnum.PAYCORE.getCode());
        balanceCommand.setRequestDate(refundBO.getRequestDate());

        //balanceCommand.setBizFeeAccount(subRefundBO.getAmt());
        balanceCommand.setBizChargeAmt(subRefundBO.getAmt());
        balanceCommand.setBizType(refundBO.getBizType());
        balanceCommand.setSubBizType(refundBO.getSubBizType());
        balanceCommand.setFundingChannel(subRefundBO.getFundingChannel());
        balanceCommand.setSubFundingChannel(subRefundBO.getSubFundingChannel());

        balanceCommand.setPayeeAccNo(subRefundBO.getPayeeAccNo());
        balanceCommand.setPayerAccNo(subRefundBO.getPayerAccNo());
        balanceCommand.setCcy(subRefundBO.getCcy());
        balanceCommand.setAmount(subRefundBO.getAmt());
        balanceCommand.setBizChargeAmt(subRefundBO.getAmt());

        balanceCommand.setAsyPayeeFlag(Boolean.FALSE);
        balanceCommand.setFundDetailNo(subRefundBO.getFundDetailNo());

        balanceCommand.setEventNo(subRefundBO.getEventNo());
        balanceCommand.setOperator(subRefundBO.getOperator());
        balanceCommand.setRemark(subRefundBO.getDescription());
        balanceCommand.setTraceLogId(MDC.get(SystemMarker.TRACE_LOG_ID));

        return balanceCommand;
    }
}
